package ai.george.toolcalling.controller;


import ai.george.toolcalling.tools.TimeTool;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.rag.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "tool-calling示例")
@RestController
@RequestMapping("/tool-calling")
public class CallingController {


    private final VectorStore vectorStore;

    private final ChatClient chatClient;

    public CallingController(VectorStore vectorStore, ChatClient.Builder builder) {


        this.vectorStore = vectorStore;

        VectorStoreDocumentRetriever vectorStoreRetriever = VectorStoreDocumentRetriever.builder()
                .vectorStore(vectorStore)
                .topK(3)
                .similarityThreshold(0.75)
                .build();

        RetrievalAugmentationAdvisor advisor = RetrievalAugmentationAdvisor.builder()
                .documentRetriever(vectorStoreRetriever)
                .build();

        this.chatClient = builder
                .defaultOptions(DashScopeChatOptions.builder().withTemperature(0.7).build())
                .defaultAdvisors(advisor)
                .build();
    }


    /**
     *  rag增强回答
     * @param question
     * @return
     */
    @GetMapping("/rag-ask-with-tool-calling")
    public String ragQuery(@RequestParam(name = "question", defaultValue = "我现在下单一杯美式，几点几分可以做好?") String question) {
        // 该chatClient已经配备了RAG能力
        return chatClient
                .prompt()
                .system("你是咖啡店的服务员，你需要回答用户的问题.")
                .user(question)
                .tools(new TimeTool())
                .call()
                .content();
    }



}
